package com.ziven.common.widget.setting;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.common.widget.R;
import com.ziven.common.widget.WidgetUtils;
import com.ziven.common.widget.value.Image;
import com.ziven.common.widget.value.Title;
import com.ziven.common.widget.window.FragmentContainerActivity;
import com.ziven.easygo.autowired.EasyGo;
import com.ziven.easygo.util.Nulls;

/**
 * @author Ziven
 */
public class ClickPreferenceItem extends BasePreferenceItem {

    public Intent intent;
    public String toast;
    public String fragment;
    public Title pageTitle;

    public ClickPreferenceItem() {
        subIcon = Image.drawableId(R.drawable.drawable_setting_item_right_go);
    }

    public Intent getIntent() {
        return intent;
    }

    public ClickPreferenceItem setIntent(Intent intent) {
        this.intent = intent;
        return this;
    }

    public String getToast() {
        return toast;
    }

    public ClickPreferenceItem setToast(String toast) {
        this.toast = toast;
        return this;
    }

    public String getFragment() {
        return fragment;
    }

    public ClickPreferenceItem setFragment(String fragment) {
        this.fragment = fragment;
        return this;
    }

    public Title getPageTitle() {
        return pageTitle;
    }

    public ClickPreferenceItem setPageTitle(Title pageTitle) {
        this.pageTitle = pageTitle;
        return this;
    }

    @NonNull
    @Override
    public String toString() {
        return "ClickPreferenceItem{" +
                "intent=" + intent +
                ", toast='" + toast + '\'' +
                ", fragment='" + fragment + '\'' +
                ", pageTitle='" + pageTitle + '\'' +
                '}';
    }


    @Override
    public boolean onItemClick(BasePreferenceItem data, int position, @NonNull View view) {
        if(getIntent() != null) {
            EasyGo.easyGo(view.getContext(), getIntent());
            return true;
        } else if(!TextUtils.isEmpty(getToast())) {
            WidgetUtils.showShort(getToast());
            return true;
        } else if(!TextUtils.isEmpty(getFragment())) {
            Title title = getPageTitle() != null ? getPageTitle() : getTitle();
            FragmentContainerActivity.startFragmentContainerActivity(getFragment(), Nulls.of(title));
            return true;
        }
        return super.onItemClick(data, position, view);
    }
}
